#include "categorymodel.h"

#include "collections/categorycollection.h"
#include "other/modelcommon.h"
#include "gameStructs.h"

CategoryModel::CategoryModel(const CategoryCollection *categories, QObject *parent) :
    _categories(categories), KeyBasedListModel(parent)
{
    Q_ASSERT(_categories);

    connect(_categories, SIGNAL(elementAdded(QString)),
            this, SLOT(addKey(QString)));
    connect(_categories, SIGNAL(elementUpdated(QString,QString)),
            this, SLOT(updateKey(QString,QString)));
    connect(_categories, SIGNAL(elementRemoved(QString)), this,
            SLOT(removeKey(QString)));
    connect(_categories, SIGNAL(cleared()), this, SLOT(clearKeys()));
}

QVariant CategoryModel::getDataForKey(const QString &key, int column, int role) const {
    if(role == Qt::DisplayRole && column == 0)
        return _categories->get(key).getName();
    return QVariant();
}

QVariant CategoryModel::headerData(int section, Qt::Orientation orientation, int role) const {
    if(section != 1 || orientation != Qt::Horizontal || role != Qt::DisplayRole)
        return QVariant();
    return tr("Name");
}
